
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  
<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/middleware/profiler/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Application Profiler &#8212; Werkzeug Documentation (1.0.x)</title>
    <link rel="stylesheet" href="../../_static/werkzeug.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../../../assets.readthedocs.org/static/css/badge_only.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script async="async" type="text/javascript" src="../../../../../assets.readthedocs.org/static/javascript/readthedocs-doc-embed.js"></script>
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
    <link rel="index" title="Index" href="../../genindex/index.html" />
    <link rel="search" title="Search" href="../../search/index.html" />
    <link rel="next" title="HTTP Exceptions" href="../../exceptions/index.html" />
    <link rel="prev" title="WSGI Protocol Linter" href="../lint/index.html" />
    <link rel="canonical" href="index.html">
  <script>DOCUMENTATION_OPTIONS.URL_ROOT = '../../index.html';</script>
   
  
<!-- RTD Extra Head -->

<!-- 
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="index.html" />

<link rel="stylesheet" href="../../../../../assets.readthedocs.org/static/css/readthedocs-doc-embed.css" type="text/css" />

<script type="text/javascript" src="../../_static/readthedocs-data.js"></script>

<!-- Add page-specific data, which must exist in the page js, not global -->
<script type="text/javascript">
READTHEDOCS_DATA['page'] = "middleware/profiler"
READTHEDOCS_DATA['source_suffix'] = ".rst"
</script>

<script type="text/javascript" src="../../../../../assets.readthedocs.org/static/javascript/readthedocs-analytics.js" async="async"></script>

<!-- end RTD <extrahead> -->
</head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex/index.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex/index.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../exceptions/index.html" title="HTTP Exceptions"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../lint/index.html" title="WSGI Protocol Linter"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">Werkzeug Documentation (1.0.x)</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Middleware</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <span class="target" id="module-werkzeug.middleware.profiler"></span><div class="section" id="application-profiler">
<h1>Application Profiler<a class="headerlink" href="#application-profiler" title="Permalink to this headline">¶</a></h1>
<p>This module provides a middleware that profiles each request with the
<a class="reference external" href="https://docs.python.org/3/library/profile.html#module-cProfile" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cProfile</span></code></a> module. This can help identify bottlenecks in your code
that may be slowing down your application.</p>
<dl class="class">
<dt id="werkzeug.middleware.profiler.ProfilerMiddleware">
<em class="property">class </em><code class="descclassname">werkzeug.middleware.profiler.</code><code class="descname">ProfilerMiddleware</code><span class="sig-paren">(</span><em>app</em>, <em>stream=&lt;_io.TextIOWrapper name='&lt;stdout&gt;' mode='w' encoding='UTF-8'&gt;</em>, <em>sort_by=('time'</em>, <em>'calls')</em>, <em>restrictions=()</em>, <em>profile_dir=None</em>, <em>filename_format='{method}.{path}.{elapsed:.0f}ms.{time:.0f}.prof'</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.middleware.profiler.ProfilerMiddleware" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrap a WSGI application and profile the execution of each
request. Responses are buffered so that timings are more exact.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">stream</span></code> is given, <a class="reference external" href="https://docs.python.org/3/library/profile.html#pstats.Stats" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">pstats.Stats</span></code></a> are written to it
after each request. If <code class="docutils literal notranslate"><span class="pre">profile_dir</span></code> is given, <a class="reference external" href="https://docs.python.org/3/library/profile.html#module-cProfile" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cProfile</span></code></a>
data files are saved to that directory, one file per request.</p>
<p>The filename can be customized by passing <code class="docutils literal notranslate"><span class="pre">filename_format</span></code>. If
it is a string, it will be formatted using <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str.format" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> with
the following fields available:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">{method}</span></code> - The request method; GET, POST, etc.</li>
<li><code class="docutils literal notranslate"><span class="pre">{path}</span></code> - The request path or ‘root’ should one not exist.</li>
<li><code class="docutils literal notranslate"><span class="pre">{elapsed}</span></code> - The elapsed time of the request.</li>
<li><code class="docutils literal notranslate"><span class="pre">{time}</span></code> - The time of the request.</li>
</ul>
<p>If it is a callable, it will be called with the WSGI <code class="docutils literal notranslate"><span class="pre">environ</span></code>
dict and should return a filename.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>app</strong> – The WSGI application to wrap.</li>
<li><strong>stream</strong> – Write stats to this stream. Disable with <code class="docutils literal notranslate"><span class="pre">None</span></code>.</li>
<li><strong>sort_by</strong> – A tuple of columns to sort stats by. See
<a class="reference external" href="https://docs.python.org/3/library/profile.html#pstats.Stats.sort_stats" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pstats.Stats.sort_stats()</span></code></a>.</li>
<li><strong>restrictions</strong> – A tuple of restrictions to filter stats by. See
<a class="reference external" href="https://docs.python.org/3/library/profile.html#pstats.Stats.print_stats" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pstats.Stats.print_stats()</span></code></a>.</li>
<li><strong>profile_dir</strong> – Save profile data files to this directory.</li>
<li><strong>filename_format</strong> – Format string for profile data file names,
or a callable returning a name. See explanation above.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">werkzeug.middleware.profiler</span> <span class="kn">import</span> <span class="n">ProfilerMiddleware</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">ProfilerMiddleware</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
</pre></div>
</div>
<details class="changelog">
<summary>Changelog</summary><div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.15: </span>Stats are written even if <code class="docutils literal notranslate"><span class="pre">profile_dir</span></code> is given, and can be
disable by passing <code class="docutils literal notranslate"><span class="pre">stream=None</span></code>.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.15: </span>Added <code class="docutils literal notranslate"><span class="pre">filename_format</span></code>.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9: </span>Added <code class="docutils literal notranslate"><span class="pre">restrictions</span></code> and <code class="docutils literal notranslate"><span class="pre">profile_dir</span></code>.</p>
</div>
</details></dd></dl>

</div>


          </div>
        </div>
      </div>
  <span id="sidebar-top"></span>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  
    
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/werkzeug.png" alt="Logo"/>
            </a></p>
  
<h3>Navigation</h3>
<ul>
  <li><a href="../../index.html">Overview</a>
    <ul>
      <li><a href="../index.html">Middleware</a>
        <ul>
          <li>Previous: <a href="../lint/index.html" title="previous chapter">WSGI Protocol Linter</a>
          <li>Next: <a href="../../exceptions/index.html" title="next chapter">HTTP Exceptions</a></ul>
      </li>
    </ul>
  </li>
</ul>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://werkzeug.palletsprojects.com/en/1.0.x/search/" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  
    <div class="footer" role="contentinfo">
        &#169; Copyright 2007 Pallets.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.5.
    </div>
    <script>
      if (typeof READTHEDOCS_DATA !== 'undefined') {
        if (!READTHEDOCS_DATA.features) {
          READTHEDOCS_DATA.features = {};
        }
        READTHEDOCS_DATA.features.docsearch_disabled = true;
      }
    </script>

  </body>

<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/middleware/profiler/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
</html>